package ro.ubb.dataaccess.service.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import ro.ubb.dataaccess.service.DataAccessServiceSuper;

public abstract class DataAccessServiceBase<T> implements DataAccessServiceSuper<T> {

	@PersistenceContext(unitName = "bsee")
	protected EntityManager em;

	private final Class<T> type;

	protected DataAccessServiceBase(Class<T> clazz) {
		type = clazz;
	}

	public List<T> getAll() {
		String selectAllSql = "SELECT entity FROM " + type.getSimpleName() + " entity ";
		TypedQuery<T> query = em.createQuery(selectAllSql, type);
		return query.getResultList();
	}

	public T getbyId(Long id) {
		return em.find(type, id);
	}

	public void save(T entity) {
		em.persist(entity);
	}

	public void delete(T entity) {
		em.remove(entity);
	}

	protected String like(String param) {
		return "%" + param + "%";
	}

}
